package me.healthcare.activity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONObject;

import me.healthcare.app.R;
import me.healthcare.common.ActionBarUtils;
import me.healthcare.common.GraphFactory;
import me.healthcare.model.Patient;
import me.healthcare.webserivce.WebService;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PatientActivity extends Activity {

	// views
	TextView patient_name;
	TextView patient_birthday;
	TextView patient_sex;
	TextView patient_blood;
	TextView patient_status;
	TextView patient_active_code;
	TextView patient_doctor;

	TextView patient_chat_link;
	TextView blood_sugar_link;
	TextView blood_pressure_link;
	LinearLayout linear_graph;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set View to login.xml
		setContentView(R.layout.patient_screen);

		// init actionbar
		ActionBarUtils.remade(this,
				getResources().getString(R.string.activity_patient));

		loadViews();
		long id = (Long) getIntent().getSerializableExtra("id");
		new getPatient().execute("http://" + WebService.HOST_IP
				+ "/sworldwebservice/sworld/auth/GetPatientInfoByAppId/" + id);

		patient_chat_link.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO
				Toast.makeText(PatientActivity.this, "Hiện chưa có o(╥﹏╥)o",
						Toast.LENGTH_SHORT).show();
			}
		});

		// graph switching listener
		blood_sugar_link.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				blood_sugar_link
						.setBackgroundResource(R.drawable.switch_cell_selected);
				blood_sugar_link.setTextColor(getResources().getColor(
						R.color.white));

				blood_pressure_link
						.setBackgroundResource(R.drawable.switch_cell_notselected);
				blood_pressure_link.setTextColor(getResources().getColor(
						R.color.blue_caption));
				linear_graph.removeAllViews();
				// prepare records
				double[] records = new double[30];
				// Mock test
				for (int i = 0; i < records.length; i++) {
					records[i] = 10 * Math.random();
				}
				GraphFactory.drawBloodSugarGraph(records, linear_graph,
						PatientActivity.this);
			}
		});

		// listening to forget password link.
		blood_pressure_link.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				blood_pressure_link
						.setBackgroundResource(R.drawable.switch_cell_selected);
				blood_pressure_link.setTextColor(getResources().getColor(
						R.color.white));

				blood_sugar_link
						.setBackgroundResource(R.drawable.switch_cell_notselected);
				blood_sugar_link.setTextColor(getResources().getColor(
						R.color.blue_caption));

				linear_graph.removeAllViews();
				// prepare records
				double[] records = new double[30];
				// Mock test
				for (int i = 0; i < records.length; i++) {
					records[i] = 10 * Math.random();
				}
				GraphFactory.drawBloodPressureGraph(records, linear_graph,
						PatientActivity.this);
			}
		});

		blood_sugar_link.performClick();

		// TODO
		// Patient patient = null;
		//
		// patient_name.setText(patient.getPatient_name());
		// patient_birthday.setText(patient.getPatient_birthday());
		// patient_sex.setText(patient.getPatient_sex());
		// patient_blood.setText(patient.getPatient_blood());
		// patient_status.setText(patient.getPatient_status());
		// patient_active_code.setText(patient.getPatient_active_code());
		// patient_doctor.setText(patient.getPatient_doctor());
	}

	private void loadViews() {
		// info textview
		patient_name = (TextView) findViewById(R.id.patient_name);
		patient_birthday = (TextView) findViewById(R.id.patient_birthday);
		patient_sex = (TextView) findViewById(R.id.patient_sex);
		patient_blood = (TextView) findViewById(R.id.patient_blood);
		patient_status = (TextView) findViewById(R.id.patient_status);
		patient_active_code = (TextView) findViewById(R.id.patient_active_code);
		patient_doctor = (TextView) findViewById(R.id.patient_doctor);

		// graph textview
		blood_sugar_link = (TextView) findViewById(R.id.blood_sugar_link);
		blood_pressure_link = (TextView) findViewById(R.id.blood_pressure_link);
		patient_chat_link = (TextView) findViewById(R.id.patient_chat_link);
		// layout
		linear_graph = (LinearLayout) findViewById(R.id.linear_graph);
	}

	class getPatient extends AsyncTask<String, Void, Patient> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		protected Patient doInBackground(String... data) {
			Patient temp = null;
			URL url;
			try {
				url = new URL(data[0]);
				Log.d("URL", url.toString());
				URLConnection jc;
				try {
					jc = url.openConnection();
					InputStream is = jc.getInputStream();

					// doc du lieu
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(is, "iso-8859-1"), 8);
					StringBuilder sb = new StringBuilder();
					String line = null;
					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					is.close();
					String jsonTxt = sb.toString(); // doc StringBuilder vao
													// chuoi
					JSONObject jj = new JSONObject(jsonTxt);
					String status = jj.getString("status");
					JSONObject obj = jj.getJSONObject("obj");
					String fullname = obj.getString("fullName");
					String birthday = obj.getString("birthday");
					String sex = obj.getString("sex");
					String bloodGroup = obj.getString("bloodgroup");
					long doctorId = obj.getLong("doctorId");
					String activecode = obj.getString("activecode");

					temp = new Patient(fullname, birthday, sex, bloodGroup,
							status, activecode, doctorId + "");
				} catch (Exception e) {
					Log.d("ERROR", e.toString());
				}

			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			}
			return temp;
		}

		protected void onPostExecute(Patient patient) {
			patient_name.setText(patient.getPatient_name());
			Log.d("name", patient.getPatient_name());
			patient_birthday.setText(patient.getPatient_birthday());
			patient_sex.setText(patient.getPatient_sex());
			Log.d("sex", patient.getPatient_sex());
			patient_blood.setText(patient.getPatient_blood());
			patient_status.setText(patient.getPatient_status());
			patient_active_code.setText(patient.getPatient_active_code());
			patient_doctor.setText(patient.getPatient_doctor());
		}
	}
}
